package com.shop.controller;

import com.shop.pojo.GoodsOrderRemark;
import com.shop.pojo.User;
import com.shop.result.JsonResult;
import com.shop.service.GoodsOrderRemarkService;
import com.shop.service.LoginService;
import com.shop.util.BaseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/orderRemark")
public class GoodsOrderRemarkController {
    @Autowired
    private GoodsOrderRemarkService goodsOrderRemarkService;
    @Autowired
    private LoginService loginService;

    @RequestMapping("/queryGoodsRemarkByCondition")
    @ResponseBody
    public JsonResult queryGoodsRemarkByCondition(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String data = request.getParameter("data");
        String token = request.getParameter("token");
        User user =loginService.getUserByToken(token);

        Map<String,Object> params = null;
        if(StringUtils.isNotEmpty(data)){
            params = BaseUtil.json2Map(data);
        }else{
            params = new HashMap<>();
        }
        params.put("userId",user.getUserId());

        List<GoodsOrderRemark> goodsOrderRemarkList = goodsOrderRemarkService.queryGoodsRemarkByCondition(params);
        return new JsonResult(true,goodsOrderRemarkList);
    }

    @RequestMapping("/addGoodsRemark")
    @ResponseBody
    public JsonResult addGoodsRemark(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String returnMsg = goodsOrderRemarkService.addGoodsRemark(request,response);
        if(!returnMsg.equals("success")){
            return new JsonResult(false,returnMsg);
        }

        return new JsonResult(true,"添加成功！");
    }

    @RequestMapping("/deleteGoodsRemark")
    @ResponseBody
    public JsonResult deleteGoodsRemark(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String returnMsg = goodsOrderRemarkService.deleteGoodsRemark(request,response);
        if(!returnMsg.equals("success")){
            return new JsonResult(false,returnMsg);
        }

        return new JsonResult(true,"删除成功！");
    }
}
